package com.geovis.emergency.spd.biz.filing.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geovis.emergency.spd.biz.filing.mapper.FilingSystemDeptMapper;
import com.geovis.emergency.spd.biz.filing.mapper.TbPlanFilingsMapper;
import com.geovis.emergency.spd.biz.filing.service.IFilingSystemDeptService;
import com.geovis.emergency.spd.biz.filing.service.ITbPlanFilingsService;
import com.geovis.emergency.spd.entity.filing.entity.FilingSystemDept;
import com.geovis.emergency.spd.entity.filing.entity.FilingSystemFileBusiness;
import com.geovis.emergency.spd.entity.filing.entity.TbPlanFilings;
import com.geovis.emergency.spd.entity.system.entity.SystemDept;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
@Slf4j
@DS("filing")
public class FilingSystemDeptServiceImpl extends ServiceImpl<FilingSystemDeptMapper, FilingSystemDept> implements IFilingSystemDeptService {

    private final TbPlanFilingsMapper tbPlanFilingsMapper;

    @Override
    public void saveFromSpd(SystemDept systemDept) {
        FilingSystemDept filingSystemDept=new FilingSystemDept();
        BeanUtils.copyProperties(systemDept,filingSystemDept);
        filingSystemDept.setTypeGrade("3");
        this.save(filingSystemDept);
    }

    @Override
    public void updateFromSpd(SystemDept systemDept,String oldDeptName) {
        FilingSystemDept filingSystemDept=this.getOne(Wrappers.lambdaQuery(FilingSystemDept.class).eq(FilingSystemDept::getDeptName,oldDeptName));
        FilingSystemDept updateEntity=new FilingSystemDept();
        BeanUtils.copyProperties(systemDept,updateEntity);
        updateEntity.setId(filingSystemDept.getId());
        this.updateById(updateEntity);
    }

    @Override
    public void removeFromSpd(List<SystemDept> systemDept) {

        removeFilingSystemDept(getFilingDepts(systemDept));
    }

    private List<FilingSystemDept> getFilingDepts(List<SystemDept> systemDepts)
    {
        List<String> names=systemDepts.stream().map(SystemDept::getDeptName).collect(Collectors.toList());
        return this.list(Wrappers.lambdaQuery(FilingSystemDept.class).in(FilingSystemDept::getDeptName,names));
    }

    @Override
    public boolean checkFilingRecord(List<SystemDept> systemDepts) {
        List<FilingSystemDept> filingSystemDepts=getFilingDepts(systemDepts);
        boolean isOk=false;
        for (FilingSystemDept dept:filingSystemDepts)
        {
            Long count=tbPlanFilingsMapper.selectCount(Wrappers.lambdaQuery(TbPlanFilings.class).eq(TbPlanFilings::getPreparationUnitId,dept.getId()));
            if(count>0)
            {
                isOk=true;
                break;
            }
            Integer num=tbPlanFilingsMapper.countFilingUnits(dept.getId());
            if(num>0)
            {
                isOk=true;
                break;
            }

        }
        return isOk;
    }

    private void removeFilingSystemDept(List<FilingSystemDept> filingSystemDept)
    {
        if(CollectionUtils.isEmpty(filingSystemDept))
        {
            return;
        }
        List<String> idList=filingSystemDept.stream().map(FilingSystemDept::getId).collect(Collectors.toList());
        removeByIds(idList);
        List<FilingSystemDept> child = baseMapper.selectList(Wrappers.lambdaQuery(FilingSystemDept.class).select(FilingSystemDept::getId).in(FilingSystemDept::getParentId, idList));
        removeFilingSystemDept(child);

    }
}
